import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/config/Config.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/model/ProductContentModel.dart';
import 'package:flutter_yishop/pages/product/product_content/TabProduct.dart';
import 'package:flutter_yishop/pages/product/product_content/TabProductDetails.dart';
import 'package:flutter_yishop/pages/product/product_content/TabProductEvaluation.dart';
import 'package:flutter_yishop/pages/tabs/Tabs.dart';
import 'package:flutter_yishop/provider/Cart.dart';
import 'package:flutter_yishop/services/CartServices.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';
import 'package:flutter_yishop/widget/JDButton.dart';
import 'package:flutter_yishop/widget/LoadingWidget.dart';
import 'package:provider/provider.dart';

class ProductContentPage extends StatefulWidget {
  Map arguments;

  ProductContentPage({this.arguments});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ProductContentPageState();
  }
}

class _ProductContentPageState extends State<ProductContentPage> {
  List _productContentList = [];
  var cartProvider;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getContentData();
  }

  _getContentData() async {
    var api = '${Config.url}api/pcontent?id=${widget.arguments['id']}';

    var result = await Dio().get(api);
    var productContent = ProductContentModel.fromJson(result.data);
    setState(() {
      this._productContentList.add(productContent.result);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    cartProvider = Provider.of<Cart>(context);
    return DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  width: ScreenAdapter.width(400),
                  child: TabBar(
                    indicatorColor: Colors.red,
                    indicatorSize: TabBarIndicatorSize.label,
                    tabs: <Widget>[
                      Tab(
                        child: Text('商品'),
                      ),
                      Tab(
                        child: Text('详情'),
                      ),
                      Tab(
                        child: Text('评价'),
                      ),
                    ],
                  ),
                )
              ],
            ),
            actions: [
              IconButton(
                  icon: Icon(Icons.more_horiz),
                  onPressed: () {
                    showMenu(
                        context: context,
                        position: RelativeRect.fromLTRB(
                            ScreenAdapter.width(600), 76, 10, 0),
                        items: [
                          PopupMenuItem(
                            child: GestureDetector(
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.home),
                                  Text("首页")
                                ],
                              ),
                              onTap: () {
                                //返回到根
                                Navigator.of(context).pushAndRemoveUntil(
                                    new MaterialPageRoute(builder: (context) => Tabs(index: 0,)),
                                        (route) => route == null);
                              },
                            ),
                          ),
                          PopupMenuItem(
                            child: GestureDetector(
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.search),
                                  Text("搜索")
                                ],
                              ),
                              onTap: () {
                                Navigator.of(context)
                                    .pushNamed(Constants.SEARCH);
                              },
                            ),
                          )
                        ]);
                  }),
            ],
          ),
          body: this._productContentList.length > 0
              ? Stack(
                  children: [
                    TabBarView(children: <Widget>[
                      TabProductPage(this._productContentList),
                      TabProductDetailsPage(this._productContentList),
                      TabProductEvaluationPage()
                    ],
                      //禁止滑动
                    physics: NeverScrollableScrollPhysics(), ),
                    Positioned(
                      width: ScreenAdapter.width(750),
                      height: ScreenAdapter.height(100),
                      bottom: 0,
                      child: Container(
                        decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border(
                              top: BorderSide(color: Colors.black26, width: 1),
                            )),
                        child: Container(
                          margin: EdgeInsets.only(
                              left: ScreenAdapter.width(15),
                              right: ScreenAdapter.width(15)),
                          child: Row(
                            children: [
                              GestureDetector(
                                child: Container(
                                  padding: EdgeInsets.only(
                                      top: ScreenAdapter.height(10)),
                                  child: Column(
                                    children: [
                                      Icon(Icons.shopping_cart),
                                      Text(
                                        "购物车",
                                        style: TextStyle(fontSize: 10),
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: () {
                                 Navigator.of(context).pushNamed(Constants.SHOPCART);
                                },
                              ),
                              SizedBox(
                                width: ScreenAdapter.width(15),
                              ),
                              Expanded(
                                flex: 1,
                                child: JdButton(
                                  color: Colors.red,
                                  text: "加入购物车",
                                  cb: () async {
                                    if (this._productContentList[0].attr.length > 0) {
                                      eventBus.fire(ProductContentEvent("加入购物车"));
                                    } else {
                                      await CartServices.addCart(this._productContentList[0]);
                                      cartProvider.updateCartList();
                                      ToastUtil.showToasts("添加成功");
                                    }
                                  },
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: JdButton(
                                  color: Color.fromRGBO(255, 165, 0, 0.9),
                                  text: "立即购买",
                                  cb: () {
                                    if (this
                                            ._productContentList[0]
                                            .attr
                                            .length >
                                        0) {
                                      eventBus
                                          .fire(ProductContentEvent("立即购买"));
                                    } else {
                                      print('立即购买操作');
                                    }
                                  },
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                )
              : LoadingWidget(),
        ));
  }
}
